home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / man / lib / tcl / TraceVar.man < prev   
Encoding:
Text File  |  1992-08-07  |  16.7 KB  |  507 lines

  1. '\"
  2. '\" Copyright 1989 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. '\" $Header: /user6/ouster/tcl/man/RCS/TraceVar.man,v 1.8 92/05/22 08:19:50 ouster Exp $ SPRITE (Berkeley)
  11. '\" 
  12. .\" The definitions below are for supplemental macros used in Sprite
  13. .\" manual entries.
  14. .\"
  15. .\" .HS name section [date [version]]
  16. .\"    Replacement for .TH in other man pages.  See below for valid
  17. .\"    section names.
  18. .\"
  19. .\" .AP type name in/out [indent]
  20. .\"    Start paragraph describing an argument to a library procedure.
  21. .\"    type is type of argument (int, etc.), in/out is either "in", "out",
  22. .\"    or "in/out" to describe whether procedure reads or modifies arg,
  23. .\"    and indent is equivalent to second arg of .IP (shouldn't ever be
  24. .\"    needed;  use .AS below instead)
  25. .\"
  26. .\" .AS [type [name]]
  27. .\"    Give maximum sizes of arguments for setting tab stops.  Type and
  28. .\"    name are examples of largest possible arguments that will be passed
  29. .\"    to .AP later.  If args are omitted, default tab stops are used.
  30. .\"
  31. .\" .BS
  32. .\"    Start box enclosure.  From here until next .BE, everything will be
  33. .\"    enclosed in one large box.
  34. .\"
  35. .\" .BE
  36. .\"    End of box enclosure.
  37. .\"
  38. .\" .VS
  39. .\"    Begin vertical sidebar, for use in marking newly-changed parts
  40. .\"    of man pages.
  41. .\"
  42. .\" .VE
  43. .\"    End of vertical sidebar.
  44. .\"
  45. .\" .DS
  46. .\"    Begin an indented unfilled display.
  47. .\"
  48. .\" .DE
  49. .\"    End of indented unfilled display.
  50. .\"
  51. '\"    # Heading for Sprite man pages
  52. .de HS
  53. .if '\\$2'cmds'       .TH \\$1 1 \\$3 \\$4
  54. .if '\\$2'lib'        .TH \\$1 3 \\$3 \\$4
  55. .if '\\$2'tcl'        .TH \\$1 3 \\$3 \\$4
  56. .if '\\$2'tk'         .TH \\$1 3 \\$3 \\$4
  57. .if t .wh -1.3i ^B
  58. .nr ^l \\n(.l
  59. .ad b
  60. ..
  61. '\"    # Start an argument description
  62. .de AP
  63. .ie !"\\$4"" .TP \\$4
  64. .el \{\
  65. .   ie !"\\$2"" .TP \\n()Cu
  66. .   el          .TP 15
  67. .\}
  68. .ie !"\\$3"" \{\
  69. .ta \\n()Au \\n()Bu
  70. \&\\$1    \\fI\\$2\\fP    (\\$3)
  71. .\".b
  72. .\}
  73. .el \{\
  74. .br
  75. .ie !"\\$2"" \{\
  76. \&\\$1    \\fI\\$2\\fP
  77. .\}
  78. .el \{\
  79. \&\\fI\\$1\\fP
  80. .\}
  81. .\}
  82. ..
  83. '\"    # define tabbing values for .AP
  84. .de AS
  85. .nr )A 10n
  86. .if !"\\$1"" .nr )A \\w'\\$1'u+3n
  87. .nr )B \\n()Au+15n
  88. .\"
  89. .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
  90. .nr )C \\n()Bu+\\w'(in/out)'u+2n
  91. ..
  92. '\"    # BS - start boxed text
  93. '\"    # ^y = starting y location
  94. '\"    # ^b = 1
  95. .de BS
  96. .br
  97. .mk ^y
  98. .nr ^b 1u
  99. .if n .nf
  100. .if n .ti 0
  101. .if n \l'\\n(.lu\(ul'
  102. .if n .fi
  103. ..
  104. '\"    # BE - end boxed text (draw box now)
  105. .de BE
  106. .nf
  107. .ti 0
  108. .mk ^t
  109. .ie n \l'\\n(^lu\(ul'
  110. .el \{\
  111. .\"    Draw four-sided box normally, but don't draw top of
  112. .\"    box if the box started on an earlier page.
  113. .ie !\\n(^b-1 \{\
  114. \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  115. .\}
  116. .el \}\
  117. \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
  118. .\}
  119. .\}
  120. .fi
  121. .br
  122. .nr ^b 0
  123. ..
  124. '\"    # VS - start vertical sidebar
  125. '\"    # ^Y = starting y location
  126. '\"    # ^v = 1 (for troff;  for nroff this doesn't matter)
  127. .de VS
  128. .mk ^Y
  129. .ie n 'mc \s12\(br\s0
  130. .el .nr ^v 1u
  131. ..
  132. '\"    # VE - end of vertical sidebar
  133. .de VE
  134. .ie n 'mc
  135. .el \{\
  136. .ev 2
  137. .nf
  138. .ti 0
  139. .mk ^t
  140. \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
  141. .sp -1
  142. .fi
  143. .ev
  144. .\}
  145. .nr ^v 0
  146. ..
  147. '\"    # Special macro to handle page bottom:  finish off current
  148. '\"    # box/sidebar if in box/sidebar mode, then invoked standard
  149. '\"    # page bottom macro.
  150. .de ^B
  151. .ev 2
  152. 'ti 0
  153. 'nf
  154. .mk ^t
  155. .if \\n(^b \{\
  156. .\"    Draw three-sided box if this is the box's first page,
  157. .\"    draw two sides but no top otherwise.
  158. .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  159. .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
  160. .\}
  161. .if \\n(^v \{\
  162. .nr ^x \\n(^tu+1v-\\n(^Yu
  163. \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
  164. .\}
  165. .bp
  166. 'fi
  167. .ev
  168. .if \\n(^b \{\
  169. .mk ^y
  170. .nr ^b 2
  171. .\}
  172. .if \\n(^v \{\
  173. .mk ^Y
  174. .\}
  175. ..
  176. '\"    # DS - begin display
  177. .de DS
  178. .RS
  179. .nf
  180. .sp
  181. ..
  182. '\"    # DE - end display
  183. .de DE
  184. .fi
  185. .RE
  186. .sp .5
  187. ..
  188. .HS Tcl_TraceVar tcl
  189. .VS
  190. .BS
  191. .SH NAME
  192. Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
  193. .SH SYNOPSIS
  194. .nf
  195. \fB#include <tcl.h>\fR
  196. .sp
  197. int
  198. \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  199. .sp
  200. int
  201. \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  202. .sp
  203. \fBTcl_UnTraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  204. .sp
  205. \fBTcl_UnTraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  206. .sp
  207. ClientData
  208. \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
  209. .sp
  210. ClientData
  211. \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
  212. .SH ARGUMENTS
  213. .AS Tcl_VarTraceProc prevClientData
  214. .AP Tcl_Interp *interp in
  215. Interpreter containing variable.
  216. .AP char *varName in
  217. Name of variable.  May refer to a scalar variable, to
  218. an array variable with no index, or to an array variable
  219. with a parenthesized index.
  220. .AP int flags in
  221. OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
  222. TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY.  Not all flags are used by all
  223. procedures.  See below for more information.
  224. .AP Tcl_VarTraceProc *proc in
  225. Procedure to invoke whenever one of the traced operations occurs.
  226. .AP ClientData clientData in
  227. Arbitrary one-word value to pass to \fIproc\fR.
  228. .AP char *name1 in
  229. Name of scalar or array variable (without array index).
  230. .AP char *name2 in
  231. For a trace on an element of an array, gives the index of the
  232. element.  For traces on scalar variables or on whole arrays,
  233. is NULL.
  234. .AP ClientData prevClientData in
  235. If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
  236. \fBTcl_VarTraceInfo2\fR, so this call will return information about
  237. next trace.  If NULL, this call will return information about first
  238. trace.
  239. .BE
  240.  
  241. .SH DESCRIPTION
  242. .PP
  243. \fBTcl_TraceVar\fR allows a C procedure to monitor and control
  244. access to a Tcl variable, so that the C procedure is invoked
  245. whenever the variable is read or written or unset.
  246. If the trace is created successfully then \fBTcl_TraceVar\fR returns
  247. TCL_OK.  If an error occurred (e.g. \fIvarName\fR specifies an element
  248. of an array, but the actual variable isn't an array) then TCL_ERROR
  249. is returned and an error message is left in \fIinterp->result\fR.
  250. .PP
  251. The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
  252. trace procedure is to be invoked and provides information
  253. for setting up the trace.  It consists of an OR-ed combination
  254. of any of the following values:
  255. .TP
  256. \fBTCL_GLOBAL_ONLY\fR
  257. Normally, the variable will be looked up at the current level of
  258. procedure call;  if this bit is set then the variable will be looked
  259. up at global level, ignoring any active procedures.
  260. .TP
  261. \fBTCL_TRACE_READS\fR
  262. Invoke \fIproc\fR whenever an attempt is made to read the variable.
  263. .TP
  264. \fBTCL_TRACE_WRITES\fR
  265. Invoke \fIproc\fR whenever an attempt is made to modify the variable.
  266. .TP
  267. \fBTCL_TRACE_UNSETS\fR
  268. Invoke \fIproc\fR whenever the variable is unset.
  269. A variable may be unset either explicitly by an \fBunset\fR command,
  270. or implicitly when a procedure returns (its local variables are
  271. automatically unset) or when the interpreter is deleted (all
  272. variables are automatically unset).
  273. .PP
  274. Whenever one of the specified operations occurs on the variable,
  275. \fIproc\fR will be invoked.
  276. It should have arguments and result that match the type
  277. \fBTcl_VarTraceProc\fR:
  278. .nf
  279. .RS
  280. typedef char *Tcl_VarTraceProc(
  281. .RS
  282. ClientData \fIclientData\fR,
  283. Tcl_Interp *\fIinterp\fR,
  284. char *\fIname1\fR,
  285. char *\fIname2\fR,
  286. int \fIflags\fR);
  287. .RE
  288. .RE
  289. .fi
  290. The \fIclientData\fP and \fIinterp\fP parameters will
  291. have the same values as those passed to \fBTcl_TraceVar\fR when the
  292. trace was created.
  293. \fIClientData\fR typically points to an application-specific
  294. data structure that describes what to do when \fIproc\fR
  295. is invoked.
  296. \fIName1\fR and \fIname2\fR give the name of the traced variable
  297. in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
  298. below for details).
  299. \fIFlags\fR is an OR-ed combination of bits providing several
  300. pieces of information.
  301. One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
  302. will be set in \fIflags\fR to indicate which operation is being performed
  303. on the variable.
  304. The bit TCL_GLOBAL_ONLY will be set whenever the variable being
  305. accessed is a global one not accessible from the current level of
  306. procedure call:  the trace procedure will need to pass this flag
  307. back to variable-related procedures like \fBTcl_GetVar\fR if it
  308. attempts to access the variable.
  309. The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
  310. about to be destroyed;  this information may be useful to \fIproc\fR
  311. so that it can clean up its own internal data structures (see
  312. the section TCL_TRACE_DESTROYED below for more details).
  313. Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
  314. interpreter is being destroyed.
  315. When this bit is set, \fIproc\fR must be especially careful in
  316. the things it does (see the section TCL_INTERP_DESTROYED below).
  317. The trace procedure's return value should normally be NULL;  see
  318. ERROR RETURNS below for information on other possibilities.
  319. .PP
  320. \fBTcl_UntraceVar\fR may be used to remove a trace.
  321. If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
  322. has a trace set with \fIflags\fR, \fIproc\fR, and
  323. \fIclientData\fR, then the corresponding trace is removed.
  324. If no such trace exists, then the call to \fBTcl_UntraceVar\fR
  325. has no effect.
  326. The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVars\fR.
  327. .PP
  328. \fBTcl_VarTraceInfo\fR may be used to retrieve information about
  329. traces set on a given variable.
  330. The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
  331. associated with a particular trace.
  332. The trace must be on the variable specified by the \fIinterp\fR,
  333. \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
  334. bit from \fIflags\fR is used;  other bits are ignored) and its trace procedure
  335. must the same as the \fIproc\fR argument.
  336. If the \fIprevClientData\fR argument is NULL then the return
  337. value corresponds to the first (most recently created) matching
  338. trace, or NULL if there are no matching traces.
  339. If the \fIprevClientData\fR argument isn't NULL, then it should
  340. be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
  341. In this case, the new return value will correspond to the next
  342. matching trace after the one whose \fIclientData\fR matches
  343. \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
  344. or if there are no more matching traces after it.
  345. This mechanism makes it possible to step through all of the
  346. traces for a given variable that have the same \fIproc\fR.
  347.  
  348. .SH "TWO-PART NAMES"
  349. .PP
  350. The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
  351. \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
  352. \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
  353. except that the name of the variable has already been
  354. separated by the caller into two parts.
  355. \fIName1\fR gives the name of a scalar variable or array,
  356. and \fIname2\fR gives the name of an element within an
  357. array.
  358. If \fIname2\fR is NULL it means that either the variable is
  359. a scalar or the trace is to be set on the entire array rather
  360. than an individual element (see WHOLE-ARRAY TRACES below for
  361. more information).
  362.  
  363. .SH "ACCESSING VARIABLES DURING TRACES"
  364. .PP
  365. During read and write traces, the
  366. trace procedure can read or write the value of the traced
  367. variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
  368. other procedures.
  369. While \fIproc\fR is executing, traces are temporarily disabled
  370. for the variable, so that calls to \fBTcl_GetVar2\fR and
  371. \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
  372. to be invoked again.
  373. Disabling only occurs for the variable whose trace procedure
  374. is active;  accesses to other variables will still be traced.
  375. .PP
  376. During unset traces the variable has already been completely
  377. expunged.
  378. It is possible for the trace procedure to read or write the
  379. variable, but this will be a new version of the variable.
  380. Traces are not disabled during unset traces as they are for
  381. read and write traces, but existing traces have been removed
  382. from the variable before any trace procedures are invoked.
  383. If new traces are set by unset trace procedures, these traces
  384. will be invoked on accesses to the variable by the trace
  385. procedures.
  386.  
  387. .SH "CALLBACK TIMING"
  388. .PP
  389. When read tracing has been specified for a variable, the trace
  390. procedure will be invoked whenever the variable's value is
  391. read.  This includes \fBset\fR Tcl commands, \fB$\fR-notation
  392. in Tcl commands, and invocations of the \fBTcl_GetVar\fR
  393. and \fBTcl_GetVar2\fR procedures.
  394. \fIProc\fR is invoked just before the variable's value is
  395. returned.
  396. It may modify the value of the variable to affect what
  397. is returned by the traced access.
  398. .PP
  399. When write tracing has been specified for a variable, the
  400. trace procedure will be invoked whenever the variable's value
  401. is modified.  This includes \fBset\fR commands\fR,
  402. commands that modify variables as side effects (such as
  403. \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
  404. and \fBTcl_SetVar2\fR procedures).
  405. \fIProc\fR will be invoked after the variable's value has been
  406. modified, but before the new value of the variable has been
  407. returned.
  408. It may modify the value of the variable to override the change
  409. and to determine the value actually returned by the traced
  410. access.
  411. .PP
  412. When unset tracing has been specified, the trace procedure
  413. will be invoked whenever the variable is destroyed.
  414. The traces will be called after the variable has been
  415. completely unset.
  416.  
  417. .SH "WHOLE-ARRAY TRACES"
  418. .PP
  419. If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
  420. the name of an array variable without an index into the array,
  421. then the trace will be set on the array as a whole.
  422. This means that \fIproc\fR will be invoked whenever any
  423. element of the array is accessed in the ways specified by
  424. \fIflags\fR.
  425. When an array is unset, a whole-array trace will be invoked
  426. just once, with \fIname1\fR equal to the name of the array
  427. and \fIname2\fR NULL;  it will not be invoked once for each
  428. element.
  429.  
  430. .SH "MULTIPLE TRACES"
  431. .PP
  432. It is possible for multiple traces to exist on the same variable.
  433. When this happens, all of the trace procedures will be invoked on each
  434. access, in order from most-recently-created to least-recently-created.
  435. When there exist whole-array traces for an array as well as
  436. traces on individual elements, the whole-array traces are invoked
  437. before the individual-element traces.
  438.  
  439. .SH "ERROR RETURNS"
  440. .PP
  441. Under normal conditions trace procedures should return NULL, indicating
  442. successful completion.
  443. If \fIproc\fR returns a non-NULL value it signifies that an
  444. error occurred.
  445. The return value must be a pointer to a static character string
  446. containing an error message.
  447. If a trace procedure returns an error, no further traces are
  448. invoked for the access and the traced access aborts with the
  449. given message.
  450. Trace procedures can use this facility to make variables
  451. read-only, for example (but note that the value of the variable
  452. will already have been modified before the trace procedure is
  453. called, so the trace procedure will have to restore the correct
  454. value).
  455. .PP
  456. The return value from \fIproc\fR is only used during read and
  457. write tracing.
  458. During unset traces, the return value is ignored and all relevant
  459. trace procedures will always be invoked.
  460.  
  461. .SH "RESTRICTIONS"
  462. .PP
  463. It is not legal to delete a variable while a trace procedure
  464. is active for the variable.
  465.  
  466. .SH "UNDEFINED VARIABLES"
  467. .PP
  468. It is legal to set a trace on an undefined variable.
  469. The variable will still appear to be undefined until the
  470. first time its value is set.
  471. If an undefined variable is traced and then unset, the unset will fail
  472. with an error (``no such variable''), but the trace
  473. procedure will still be invoked.
  474.  
  475. .SH "TCL_TRACE_DELETED FLAG"
  476. .PP
  477. In an unset callback to \fIproc\fR, the TCL_TRACE_DELETED bit
  478. is set in \fIflags\fR if the trace is being removed as part
  479. of the deletion.
  480. Traces on a variable are always removed whenever the variable
  481. is deleted;  the only time TCL_TRACE_DELETED isn't set is for
  482. a whole-array trace invoked when only a single element of an
  483. array is unset.
  484.  
  485. .SH "TCL_INTERP_DESTROYED"
  486. .PP
  487. When an interpreter is destroyed, unset traces are called for
  488. all of its variables.
  489. The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
  490. argument passed to the trace procedures.
  491. Trace procedures must be extremely careful in what they do if
  492. the TCL_INTERP_DESTROYED bit is set.
  493. It is not safe for the procedures to invoke any Tcl procedures
  494. on the interpreter, since its state is partially deleted.
  495. All that trace procedures should do under these circumstances is
  496. to clean up and free their own internal data structures.
  497.  
  498. .SH BUGS
  499. .PP
  500. Tcl doesn't do any error checking to prevent trace procedures
  501. from misusing the interpreter during traces with TCL_INTERP_DESTROYED
  502. set.
  503.  
  504. .SH KEYWORDS
  505. clientData, trace, variable
  506. .VE
  507.